const express = require("express");
const router = express.Router();
const Response = require("../utils/Response.js");
const fs = require("fs");

// 引入mysql连接池
const pool = require("../utils/db.js");

// 新增分享内容
router.post("/add", (req, res, next) => {
  let obj = req.body;
  pool.query("insert into share set ?", [obj], (err, result) => {
    if (err) {
      return next(err);
    }
    console.log(result);
    if (result.affectedRows === 0) {
      res.send({ code: 201, msg: "新增失败" });
    } else {
      res.send({ code: 200, msg: "新增成功" });
    }
  });
});

//  通过sid查询发布内容
router.get("/", (req, res, next) => {
  let obj = req.query;
  console.log(obj);
  pool.query("select * from share where sid=?", [obj.sid], (err, result) => {
    if (err) {
      return next(err);
    }
    console.log(result);
    //结果是对象，如果对象下的属性length为0说明修改失败
    if (result.length === 0) {
      res.send({ code: 201, msg: "查询失败" });
    } else {
      res.send({ code: 200, msg: "查询成功", data: result });
    }
  });
});

//
router.get("/byuid", (req, res, next) => {
  let obj = req.query;
  console.log(obj);
  pool.query(
    "select share.*,user.headpic,user.uname from share,user where share.uid=? and share.uid=user.uid",
    [obj.uid],
    (err, result) => {
      if (err) {
        return next(err);
      }
      console.log(result);
      //结果是对象，如果对象下的属性length为0说明修改失败
      if (result.length === 0) {
        res.send({ code: 201, msg: "查询失败" });
      } else {
        res.send({ code: 200, msg: "查询成功", data: result });
      }
    }
  );
});

// 查询发布分享的用户的信息
router.get("/user", (req, res, next) => {
  let obj = req.query;
  console.log(obj);
  pool.query(
    "select user.headpic,user.uname from share,user where share.uid=? and share.uid=user.uid",
    [obj.uid],
    (err, result) => {
      if (err) {
        return next(err);
      }
      console.log(result);
      //结果是对象，如果对象下的属性length为0说明修改失败
      if (result.length === 0) {
        res.send({ code: 201, msg: "查询失败" });
      } else {
        res.send({ code: 200, msg: "查询成功", data: result });
      }
    }
  );
});

//   查询所有发布的内容
router.get("/all", (req, res, next) => {
  let obj = req.query;
  console.log(obj);
  pool.query(
    "select share.*,user.uname,user.headpic from share,user where share.uid=user.uid order by sid",
    [],
    (err, result) => {
      if (err) {
        return next(err);
      }
      console.log(result);
      //结果是对象，如果对象下的属性length为0说明修改失败
      if (result.length === 0) {
        res.send({ code: 201, msg: "查询失败" });
      } else {
        res.send({ code: 200, msg: "查询成功", data: result });
      }
    }
  );
});

// 删除分享的内容

router.post("/delete", (req, res, next) => {
  let obj = req.body;
  console.log(obj);
  pool.query("delete from share where sid=?", [obj.sid], (err, result) => {
    if (err) {
      return next(err);
    }
    if (result.affectedRows === 0) {
      res.send({ code: 201, msg: "删除失败" });
    } else {
      res.send({ code: 200, msg: "删除成功" });
    }
  });
});

// 修改发布的内容
router.post("/update", (req, res, next) => {
  let obj = req.body;
  console.log(obj);
  pool.query(
    "update share set title=?,pic=?,detail=? where sid=?",
    [obj.title, obj.pic, obj.detail, obj.sid],
    (err, result) => {
      if (err) {
        return next(err);
      } else {
        res.send({ code: 200, msg: "更新成功" });
      }
    }
  );
});

module.exports = router;
